package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.CraftPathDetail;
import com.uwlaser.service.CraftPathDetailService;
import com.uwlaser.service.dto.CraftPathDetailDto;
import com.uwlaser.service.dto.CraftPathDetailQueryParam;
import com.uwlaser.service.mapper.CraftPathDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = CraftPathDetailService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CraftPathDetailServiceImpl extends CommonServiceImpl<CraftPathDetailMapper, CraftPathDetail> implements CraftPathDetailService {

    // private final RedisUtils redisUtils;
    private final CraftPathDetailMapper craftPathDetailMapper;

    @Override
    public PageInfo<CraftPathDetailDto> queryAll(CraftPathDetailQueryParam query, Pageable pageable) {
        IPage<CraftPathDetail> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<CraftPathDetail> page = craftPathDetailMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, CraftPathDetailDto.class);
    }

    @Override
    public List<CraftPathDetailDto> queryAll(CraftPathDetailQueryParam query){
        return ConvertUtil.convertList(craftPathDetailMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), CraftPathDetailDto.class);
    }

    @Override
    public CraftPathDetail getById(Integer id) {
        return craftPathDetailMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public CraftPathDetailDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), CraftPathDetailDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(CraftPathDetailDto resources) {
        CraftPathDetail entity = ConvertUtil.convert(resources, CraftPathDetail.class);
        return craftPathDetailMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(CraftPathDetailDto resources){
        CraftPathDetail entity = ConvertUtil.convert(resources, CraftPathDetail.class);
        int ret = craftPathDetailMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return craftPathDetailMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<CraftPathDetailDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (CraftPathDetailDto craftPathDetail : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("路线ID", craftPathDetail.getPathId());
              map.put("工序编号", craftPathDetail.getOperationCode());
              map.put("工序名称", craftPathDetail.getOperationName());
              map.put("执行顺序", craftPathDetail.getExecutionOrder());
              map.put("上工序编号", craftPathDetail.getUpStationCode());
              map.put("下工序编号", craftPathDetail.getNextStationCode());
              map.put("是否校验", craftPathDetail.getIsCheck());
              map.put("是否屏蔽", craftPathDetail.getIsShield());
              map.put("描述", craftPathDetail.getRemark());
              map.put("创建人", craftPathDetail.getCreateBy());
              map.put("创建时间", craftPathDetail.getCreateTime());
              map.put("更新人", craftPathDetail.getUpdateBy());
              map.put("更新时间", craftPathDetail.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
